const TokenKey = 'token'

// 获取token
export function getToken(): string {
  const token = localStorage.getItem(TokenKey) || ''
  // 如果token包含Bearer前缀，则移除
  return token.startsWith('Bearer ') ? token.substring(7) : token
}

// 设置token
export function setToken(token: string) {
  // 确保存储的是原始token，不包含Bearer前缀
  const tokenValue = token.startsWith('Bearer ') ? token.substring(7) : token
  localStorage.setItem(TokenKey, tokenValue)
}

// 移除token
export function removeToken() {
  localStorage.removeItem(TokenKey)
}

// 检查是否有权限
export const hasPermission = (permission: string): boolean => {
  const permissions = JSON.parse(localStorage.getItem('permissions') || '[]')
  return permissions.includes(permission)
}

// 检查是否有角色
export const hasRole = (role: string): boolean => {
  const roles = JSON.parse(localStorage.getItem('roles') || '[]')
  return roles.includes(role)
}

// 检查是否已登录
export const isAuthenticated = (): boolean => {
  return !!getToken()
} 